#

# check the key words of each line in a dir. 

require "00_common"



module KeywordsStat
  def self.get_all_files(dir)
  	all_files = Common.get_all_files(dir)
  end

  def self.stat_all_files(dir)
  	keywords = ["api.facebook.com", "apps.facebook.com", "facebook.com", "www.facebook.com", "channel.facebook.com", "others"]
    keyword_mapping = Hash.new
    keywords.each do |keyword|
      keyword_mapping[keyword] = 0
    end

  	all_files = get_all_files(dir)
  	all_files.each do |file_name|
  	  file_stat = handle_each_file(dir, file_name, keywords)

      keywords.each do |keyword|
        keyword_mapping[keyword] = keyword_mapping[keyword] + file_stat[keyword]
      end
  	end

  	keyword_mapping
  end

  def self.check_outlink(referer, host)
  	
  end

  def self.handle_each_file(dir, file_name, keywords)
  	file = File.open dir + file_name, "r"

    keyword_mapping = Hash.new
    keywords.each do |keyword|
      keyword_mapping[keyword] = 0
    end

  	begin
      while line = file.readline
        fields = line.split "|"

        if fields[1].include? "api.facebook.com"
          keyword_mapping["api.facebook.com"] = keyword_mapping["api.facebook.com"] + 1
        end

        if fields[1].include? "apps.facebook.com"
          keyword_mapping["apps.facebook.com"] = keyword_mapping["apps.facebook.com"] + 1
        end
        if fields[1].include? "facebook.com"
          keyword_mapping["facebook.com"] = keyword_mapping["facebook.com"] + 1
        end

        if fields[1].include? "www.facebook.com"
          keyword_mapping["www.facebook.com"] = keyword_mapping["www.facebook.com"] + 1
        end

        if fields[1].include? "channel.facebook.com"
          keyword_mapping["channel.facebook.com"] = keyword_mapping["channel.facebook.com"] + 1
        end

        if (!fields[1].include? "facebook.com") and ((fields[2].include? "facebook.com") or (fields[2].include? "fbcdn.net") )
          keyword_mapping["others"] = keyword_mapping["others"] + 1
        end
      end
  	rescue Exception => e
  		err = e.to_s
      if err == "end of file reached"
        # nothing
      else
        puts err
      end
  	end
  	keyword_mapping
  end
end

begin
  puts "facebook_payload_svr: "
  mapping = KeywordsStat.stat_all_files("./facebook_payload_svr/")
  mapping.each do |key, value|
    puts "#{key}:#{value}"
  end
rescue Exception => e
  puts e.to_s	
end